﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ajayumi.develop.win32.GRssReader.Service.Base;
using System.Xml.Linq;
using System.ServiceModel.Syndication;

namespace ajayumi.develop.win32.GRssReader.Service.Model
{
    public class SubscriptionInfo : XElementItemBase, ISave
    {

        public string Url { get; set; }

        public string SortId { get; set; }

        public string FirstItemmsec { get; set; }

        public string htmlUrl { get; set; }
        /// <summary>
        /// 未读文章数
        /// </summary>
        public int UnReadCount { get; set; }

        public string TitleText { get; set; }
        /// <summary>
        /// 分类
        /// </summary>
        public string Category { get; set; }
        public List<string> CategoryList { get; set; }
        /// <summary>
        /// 本地相对路径
        /// </summary>
        public string RelativeLocalPath { get; set; }
        /// <summary>
        /// 本地完整路径
        /// </summary>
        public string LocalPath { get; set; }


        /// <summary>
        /// Initialize the subscription.
        /// </summary>
        /// <param name="item"></param>
        public SubscriptionInfo(XElement item)
            : base(item)
        {

        }

        protected override void LoadItem(System.Xml.Linq.XElement item)
        {
            this.CategoryList = new List<string>();

            // Regular fields.
            this.Id = GetDescendantValue(item, "string", "name", "id");
            this.TitleText = GetDescendantValue(item, "string", "name", "title");
            this.Title = new TextSyndicationContent(this.TitleText);
            this.SortId = GetDescendantValue(item, "string", "name", "sortid");
            this.FirstItemmsec = GetDescendantValue(item, "number", "name", "firstitemmsec");
            this.htmlUrl = GetDescendantValue(item, "string", "name", "htmlUrl");

            string unreadCount = GetDescendantValue(item, "number", "name", "UnReadCount");
            if (!string.IsNullOrEmpty(unreadCount))
            { this.UnReadCount = int.Parse(unreadCount); }

            // Parse the URL.
            if (Id.Contains('/'))
            { Url = Id.Substring(Id.IndexOf('/') + 1, Id.Length - Id.IndexOf('/') - 1); }

            // Get the categories.
            var catList = GetDescendant(item, "list", "name", "categories");
            if (catList != null && catList.HasElements)
            {
                var categories = GetDescendants(item, "string", "name", "label");
                this.Category = categories.First().Value;
                foreach (string category in categories.Select(o => o.Value))
                {
                    this.Categories.Add(new System.ServiceModel.Syndication.SyndicationCategory(category));
                    this.CategoryList.Add(category);
                }
            }
        }

        public void SaveItem(System.Xml.XmlWriter writer)
        {
            writer.WriteStartElement("object");

            writer.WriteStartElement("string");
            writer.WriteAttributeString("name", "id");
            writer.WriteString(this.Id);
            writer.WriteEndElement();

            writer.WriteStartElement("string");
            writer.WriteAttributeString("name", "title");
            writer.WriteString(this.TitleText);
            writer.WriteEndElement();

            writer.WriteStartElement("list");
            writer.WriteAttributeString("name", "categories");
            foreach (SyndicationCategory category in this.Categories)
            {
                writer.WriteStartElement("object");

                writer.WriteStartElement("string");
                writer.WriteAttributeString("name", "id");
                writer.WriteString(category.Name);
                writer.WriteEndElement();

                writer.WriteStartElement("string");
                writer.WriteAttributeString("name", "label");
                writer.WriteString(category.Name);
                writer.WriteEndElement();

                writer.WriteEndElement();
            }
            writer.WriteEndElement();

            writer.WriteStartElement("string");
            writer.WriteAttributeString("name", "sortid");
            writer.WriteString(this.SortId);
            writer.WriteEndElement();

            writer.WriteStartElement("number");
            writer.WriteAttributeString("name", "firstitemmsec");
            writer.WriteString(this.FirstItemmsec);
            writer.WriteEndElement();

            writer.WriteStartElement("string");
            writer.WriteAttributeString("name", "htmlUrl");
            writer.WriteString(this.htmlUrl);
            writer.WriteEndElement();

            writer.WriteStartElement("number");
            writer.WriteAttributeString("name", "UnReadCount");
            writer.WriteString(this.UnReadCount.ToString());
            writer.WriteEndElement();

            writer.WriteEndElement();
        }

    }
}
